package com.seo4j.adb4j.gui.panel;

import com.seo4j.adb4j.gui.DeviceTabPanel;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.ResourceBundle;

public class HomePanel extends JPanel {
	private static final ResourceBundle BUNDLE = ResourceBundle.getBundle("com.seo4j.adb4j.gui.panel.messages"); //$NON-NLS-1$
	
	private DeviceTabPanel gui;
	private JTextField ipField;
	private JTextField portField;
	private JTextArea textArea;
	private JTextArea areaPhones;
	private JTextArea areaSMS;
	private JTextField textField;
	private JCheckBox chckbxWaitEventTo;
	private JTextField toastField;
	private JTextField durationField;
	private JTextField urlField;

	/**
	 * Create the panel.
	 */
	public HomePanel(DeviceTabPanel gui) {
		this.gui = gui;
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, BUNDLE.getString("Informations"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), BUNDLE.getString("Quick-actions"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(null, BUNDLE.getString("Client-options"), TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 327, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
						.addComponent(panel_2, GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
					.addGap(18))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panel, GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 142, Short.MAX_VALUE)))
					.addContainerGap())
		);
		
		JLabel lblWhitephones = new JLabel(BUNDLE.getString("Phones;")); //$NON-NLS-1$
		
		JLabel lblWhitesms = new JLabel(BUNDLE.getString("SMS;")); //$NON-NLS-1$
		
		JLabel lblNeededKeyword = new JLabel(BUNDLE.getString("Needed-keywords")); //$NON-NLS-1$
		
		textField = new JTextField();
		textField.setColumns(10);
		
		areaPhones = new JTextArea();
		
		areaSMS = new JTextArea();
		
		JLabel lblServerIp = new JLabel(BUNDLE.getString("Server-IP")); //$NON-NLS-1$
		
		ipField = new JTextField();
		ipField.setHorizontalAlignment(SwingConstants.LEFT);
		ipField.setText("192.168.0.10");
		ipField.setColumns(10);
		
		JLabel lblServerPort = new JLabel(BUNDLE.getString("Server-Port")); //$NON-NLS-1$
		
		portField = new JTextField();
		portField.setText("5555");
		portField.setHorizontalAlignment(SwingConstants.LEFT);
		portField.setColumns(10);
		
		chckbxWaitEventTo = new JCheckBox(BUNDLE.getString("Wait-connect")); //$NON-NLS-1$
		
		JButton btnSaveConnectionInfo = new JButton(BUNDLE.getString("Save-conf"));
		btnSaveConnectionInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				fireButtonSaveConnectionConfig();
			}
		});
		GroupLayout gl_panel_2 = new GroupLayout(panel_2);
		gl_panel_2.setHorizontalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addGap(97)
					.addComponent(btnSaveConnectionInfo)
					.addContainerGap(107, Short.MAX_VALUE))
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addComponent(chckbxWaitEventTo)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(lblWhitephones, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
							.addGap(4)
							.addComponent(areaPhones, GroupLayout.DEFAULT_SIZE, 192, Short.MAX_VALUE))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(lblNeededKeyword)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(textField, GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(lblWhitesms)
							.addGap(18)
							.addComponent(areaSMS, GroupLayout.DEFAULT_SIZE, 213, Short.MAX_VALUE))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(lblServerIp)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(ipField, GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addComponent(lblServerPort)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(portField, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_panel_2.setVerticalGroup(
			gl_panel_2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_2.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addComponent(lblWhitephones, GroupLayout.PREFERRED_SIZE, 44, GroupLayout.PREFERRED_SIZE)
						.addComponent(areaPhones, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
					.addGroup(gl_panel_2.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_2.createSequentialGroup()
							.addGap(18)
							.addComponent(lblWhitesms)
							.addGap(12))
						.addGroup(gl_panel_2.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(areaSMS, GroupLayout.DEFAULT_SIZE, 33, Short.MAX_VALUE)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblNeededKeyword)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(ipField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblServerIp))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
						.addComponent(portField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblServerPort))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(chckbxWaitEventTo)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnSaveConnectionInfo)
					.addGap(4))
		);
		panel_2.setLayout(gl_panel_2);
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton btnNewButton = new JButton(BUNDLE.getString("Refresh")); //$NON-NLS-1$
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				fireButtonRefreshAdv();
			}
		});
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 293, Short.MAX_VALUE))
						.addGroup(gl_panel.createSequentialGroup()
							.addGap(113)
							.addComponent(btnNewButton)))
					.addContainerGap())
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnNewButton))
		);
		
		textArea = new JTextArea();
		scrollPane.setViewportView(textArea);
		panel.setLayout(gl_panel);
		
		toastField = new JTextField();
		toastField.setColumns(10);
		
		JButton btnToastIt = new JButton(BUNDLE.getString("Toast-it")); //$NON-NLS-1$
		btnToastIt.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				fireButtonToast();
			}
		});
		
		JButton btnVibrate = new JButton(BUNDLE.getString("Vibrate")); //$NON-NLS-1$
		btnVibrate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				fireButtonVibrate();
			}
		});
		
		durationField = new JTextField();
		durationField.setColumns(10);
		
		JLabel lblDuration = new JLabel(BUNDLE.getString("Duration;")); //$NON-NLS-1$
		
		JLabel lblOpenUrl = new JLabel(BUNDLE.getString("Open-url;")); //$NON-NLS-1$
		
		urlField = new JTextField();
		urlField.setColumns(10);
		
		JButton btnBrowseIt = new JButton(BUNDLE.getString("Browse-it")); //$NON-NLS-1$
		btnBrowseIt.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				fireButtonBrowse();
			}
		});
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_panel_1.createSequentialGroup()
									.addComponent(toastField, GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(btnToastIt))
								.addGroup(gl_panel_1.createSequentialGroup()
									.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
										.addComponent(lblOpenUrl)
										.addComponent(lblDuration))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
										.addGroup(gl_panel_1.createSequentialGroup()
											.addComponent(durationField, GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(btnVibrate))
										.addComponent(urlField, GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)))))
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGap(156)
							.addComponent(btnBrowseIt, GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)))
					.addContainerGap())
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(toastField, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnToastIt, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnVibrate)
						.addComponent(durationField, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblDuration))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblOpenUrl)
						.addComponent(urlField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnBrowseIt)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		panel_1.setLayout(gl_panel_1);
		setLayout(groupLayout);
	}
	
	private void fireButtonSaveConnectionConfig() {
		ArrayList<String> phones = new ArrayList<String>();
		ArrayList<String> sms = new ArrayList<String>();
		ArrayList<String> kw = new ArrayList<String>();
		
		for(String phone : areaPhones.getText().split("\n")) phones.add(phone);
		for(String s : areaSMS.getText().split("\n")) sms.add(s); 
		for(String key : textField.getText().split(" ")) kw.add(key);
		
		//gui.fireSaveConnectConfigurations(ipField.getText(), Integer.valueOf(portField.getText()), chckbxWaitEventTo.isSelected(), phones, sms, kw);
	}
	
	private void fireButtonToast() {
		String mess = toastField.getText();

	}
	
	private void fireButtonVibrate() {
		String value = durationField.getText();
		long l = Long.valueOf(value);

	}
	
	private void fireButtonBrowse() {
	}
	
	public void updatePreferences(String ip, int port, boolean wait, ArrayList<String> phones, ArrayList<String> sms, ArrayList<String> kw) {
		String temp = "";
		if(phones != null) {
			for(String s : phones) temp += s + "\n";
		}
		areaPhones.setText(temp);
		
		temp = "";
		if(sms != null) {
			for(String s : sms) temp += s + "\n";
		}
		areaSMS.setText(temp);
		
		temp = "";
		if(kw != null) {
			for(String s : kw) temp += s+" ";
			temp = temp.substring(0, temp.length() - 2);
		}
		textField.setText(temp);
		
		ipField.setText(ip);
		portField.setText(""+port);
		chckbxWaitEventTo.setSelected(wait);
	}
	
	private void fireButtonRefreshAdv() {
		textArea.setText("");
	//	gui.fireGetAdvancedInformations();
	}

}
